/********************************************************************************
 * <PRE> * 파일 정보 *
 * 1. FileName	: AjaxSessionTimeoutFilter.java
 * 2. Package	: com.ntels.bems.common.servlet
 * 3. 설명        	: TODO
 * 4. 작성자		: P044826
 * 5. 작성일		: 2014. 4. 4. 오후 1:54:40
 * 6. 설명		:
 * </PRE>
 ********************************************************************************/


package com.sunny.hp.common.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/********************************************************************************
 * <PRE> * 프로그램 정보 *
 * 1. Project		: bems20
 * 2. Package	: com.ntels.bems.common.servlet
 * 3. FileName	: AjaxSessionTimeoutFilter.java
 * 4. 작성자		: P044826
 * 5. 작성일		: 2014. 4. 4. 오후 1:54:40
 * 6. 설명			:
 * </PRE>
 ********************************************************************************/

public class AjaxSessionTimeoutFilter implements Filter{

	private String ajaxHaeder = "AJAX";
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
	    HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        //System.out.println("index 1 ==> " + req.getServletPath().indexOf("/mbd"));
        String [] pathArray = req.getServletPath().split("/");
        //System.out.println("index 2 ==> " + pathArray[1]);
        //System.out.println("================>>>>>>>" + req.getHeader(ajaxHaeder));
        //AJAX 요청인가요?
        if(req.getHeader(ajaxHaeder) != null && req.getHeader(ajaxHaeder).equals("true") && !pathArray[1].equals("mbd")) {


                	HttpSession session = req.getSession();
                	if(session.getAttribute("userSession") == null){
                		res.sendError(HttpServletResponse.SC_FORBIDDEN);
                	}else{
                		chain.doFilter(req, res);
                	}
                	System.out.println("index 3 ==> " + pathArray[1]);
        } else
        	chain.doFilter(req, res);
	}
	/**
	 * <PRE>
	 * 1. MethodName	: destroy
	 * 2. ClassName		: Filter
	 * 3. Commnet		: 
	 * 4. 작성자			: P044826
	 * 5. 작성일			: 2014. 4. 4. 오후 4:26:04
	 * </PRE>
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * <PRE>
	 * 1. MethodName	: init
	 * 2. ClassName		: Filter
	 * 3. Commnet		: 
	 * 4. 작성자			: P044826
	 * 5. 작성일			: 2014. 4. 4. 오후 4:26:04
	 * </PRE>
	 * 		@param arg0
	 * 		@throws ServletException
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
}
